package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.*;
import com.vf.admin.client.service.*;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AgentUserServiceImpl implements AgentUserService {

    @Autowired
    AgentUserDao agentUserDao;
    @Autowired
    AppUserDao appUserDao;
    @Autowired
    AppSetupDao appSetupDao;
    @Autowired
    AppUserService appUserService;
    @Autowired
    AgentUserRecordsService agentUserRecordsService;
    @Autowired
    SsAreaService areaService;
    @Autowired
    AgentUserMoneyRecordDao agentUserMoneyRecordDao;

    @Override
    public List<AgentUser> page(SplitPageBean splitPage, Map<String, Object> paraMap) {
        List<AgentUser> page = agentUserDao.page(splitPage, paraMap);
        for(AgentUser p : page) {
            SsArea area = areaService.get(p.getAreaId());
            p.setAreaName(area.getName());
            p.setPassword(null);
        }
        return page;
    }

    @Override
    public void save(AgentUser param) {
        param.setMoney(BigDecimal.ZERO);
        agentUserDao.add(param);
    }

    @Override
    public void update(AgentUser param) {
        agentUserDao.update(param);
    }

    @Override
    public AgentUser findUserByUserId(Integer userId) {
        return agentUserDao.findUserByUserId(userId);
    }

    @Override
    public AgentUser findUserByAreaIdNotMe(Integer areaId, Integer userId) {
        return agentUserDao.findUserByAreaIdNotMe(areaId, userId);
    }

    @Override
    public AgentUser findUserByAreaId(Integer areaId) {
        return agentUserDao.findUserByAreaId(areaId);
    }

    /**
     * 代理商提成
     * @param userId
     * @param money
     */
    @Override
    public void shareMoneyRecharge(Integer userId, BigDecimal money) {
        AppUser user = appUserDao.get(userId);

        /**
         * 男用户充值钻石时上级代理商参与分成
         *
         */
        if(user.getSex() == 1) {
            AgentUserRecords parent = agentUserRecordsService.findByParent(userId);
            if(parent != null) {
                AgentUser agentUser = this.findUserByUserId(parent.getParentUserId());

                BigDecimal multiply = money.multiply(new BigDecimal(agentUser.getRechargeRate().toString())).setScale(2, BigDecimal.ROUND_HALF_UP);

                //增加代理商余额
                agentUserDao.addMoney(agentUser.getId(), multiply);
                //增加代理商流水记录
                AgentUserMoneyRecord p = new AgentUserMoneyRecord();
                p.setUserId(agentUser.getId());
                p.setMoney(multiply);
                p.setType(1);
                p.setCreatetime(new Date());
                agentUserMoneyRecordDao.add(p);
            }
        }
    }

    /**
     * 代理商提成
     * @param userId
     * @param money
     */
    @Override
    public void shareMoneyWithdraw(Integer userId, BigDecimal money) {
        AppUser user = appUserDao.get(userId);

        /**
         * 女用户提现金币时上级代理商参与分成
         *
         */
        if(user.getShengid() != null && user.getShengid() != 0 && user.getSex() == 2) {
            AgentUserRecords parent = agentUserRecordsService.findByParent(userId);
            AgentUser agentUser = this.findUserByUserId(parent.getParentUserId());

            BigDecimal multiply = money.multiply(new BigDecimal(agentUser.getWithdrawRate().toString())).setScale(2, BigDecimal.ROUND_HALF_UP);

            agentUserDao.addMoney(agentUser.getId(), multiply);
            AgentUserMoneyRecord p = new AgentUserMoneyRecord();
            p.setUserId(agentUser.getId());
            p.setMoney(multiply);
            p.setType(1);
            p.setCreatetime(new Date());
            agentUserMoneyRecordDao.add(p);
        }
    }

    @Override
    public AgentUser findByUser(String username, String password) {
        return agentUserDao.findByUser(username, password);
    }

    @Override
    public AgentUser get(Integer userId) {
        return agentUserDao.get(userId, AgentUser.class);
    }

    @Override
    public BigDecimal getMoney(Integer userId) {
        AgentUser agentUser = agentUserDao.get(userId, AgentUser.class);

        return agentUser.getMoney();
    }

    @Override
    public void addMoney(Integer id, BigDecimal money) {
        agentUserDao.addMoney(id, money);
    }

    @Override
    public void subMoney(Integer id, BigDecimal money) {
        agentUserDao.subMoney(id, money);
    }
}
